package br.com.mtg.movethegarbage.screens;

import br.com.mtg.movethegarbage.game.R;
import android.app.Activity;
import android.content.Context;
import android.text.InputFilter;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import br.com.mtg.movethegarbage.framework.Game;
import br.com.mtg.movethegarbage.framework.GameTouchEvent;
import br.com.mtg.movethegarbage.framework.Input;
import br.com.mtg.movethegarbage.framework.Screen;
import br.com.mtg.movethegarbage.game.Assets;
import br.com.mtg.movethegarbage.game.Score;
import br.com.mtg.movethegarbage.game.ScoreList;
import br.com.mtg.movethegarbage.graphics.Camera2D;
import br.com.mtg.movethegarbage.graphics.SpriteManager;
import br.com.mtg.movethegarbage.impl.Button;

public class SaveScoreScreen extends Screen
{
	EditText nameScoreInput;
	SpriteManager spriteManager;
	Button exit, save;
	Score score;
	ScoreList scoreList;
	Camera2D cam;
	int mapLvl;

	public SaveScoreScreen(Game _game, int points, int _mapLvl)
	{
		super(_game);

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
		cam = new Camera2D(game.getGraphics());

		spriteManager = new SpriteManager(game.getGraphics(), 1);

		score = new Score(points, "");
		scoreList = new ScoreList(game);

		float stringWidth;
		Assets.font.setScale(1.0F * multiplier);
		stringWidth = Assets.font.getTextLength(game.getStringFromResource(R.string.exit));

		exit = new Button(width / 2 - stringWidth / 2, height / 2 - Assets.font.getTextHeight() - 20 * multiplier, game.getStringFromResource(R.string.exit),
				1.0F * multiplier, Assets.buttonBackgroundRect);

		stringWidth = Assets.font.getTextLength(game.getStringFromResource(R.string.save));

		save = new Button(width / 2 - stringWidth / 2, height / 2, game.getStringFromResource(R.string.save), 1.0F * multiplier, Assets.buttonBackgroundRect);

		((Activity) game).runOnUiThread(new Runnable() {

			public void run()
			{
				nameScoreInput = new EditText((Context) game);
				LayoutParams lp = new ViewGroup.LayoutParams((int) (130 * multiplier), (int) (40 * multiplier));
				InputFilter[] filter = { new InputFilter.LengthFilter(10) };
				nameScoreInput.setFilters(filter);
				nameScoreInput.setSingleLine();
				((Activity) game).addContentView(nameScoreInput, lp);
			}
		});
		mapLvl = _mapLvl;
	}

	@Override
	public void update(long delta)
	{
		Input input = game.getInput();
		synchronized (input)
		{
			for (GameTouchEvent e : input.getTouchEvents())
			{
				save.handleEvent(e, game);
				exit.handleEvent(e, game);
			}
			input.getTouchEvents().clear();
		}
		if (exit.wasPressed() || input.wasBackPressed())
		{
			game.setScreen(new MapScreen(game));
			((Activity) game).runOnUiThread(new Runnable() {

				public void run()
				{
					nameScoreInput.setVisibility(EditText.GONE);
				}
			});
		}
		else if (save.wasPressed())
		{
			String str = nameScoreInput.getText().toString();
			while (str.length() < 10)
			{
				str += " ";
			}
			score.name = str;
			scoreList.writeScore(score, "Level" + mapLvl);
			((Activity) game).runOnUiThread(new Runnable() {

				public void run()
				{
					nameScoreInput.setVisibility(EditText.GONE);
				}
			});
			game.setScreen(new MapScreen(game));
		}
	}

	@Override
	public void present(long delta)
	{
		spriteManager.beginDraw(Assets.tex_backgroundScreenRedMain);
		spriteManager.drawSprite(0, 0, width, height, Assets.background);
		spriteManager.endDraw();

		Assets.font.setScale(1.0F * multiplier);
		Assets.font.printAt("< " + game.getStringFromResource(R.string.name), 130 * multiplier + 10, height - Assets.font.getTextHeight());
		Assets.font.printAt(score.points + "< " + game.getStringFromResource(R.string.score), 10, height - 2 * Assets.font.getTextHeight());

		save.draw(game.getGraphics());
		exit.draw(game.getGraphics());
	}

	@Override
	public void pause()
	{
	}

	@Override
	public void resume()
	{
		cam.setViewAndMatrices();

		game.getGraphics().initialize();

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();

		game.getAudio().playMusic(Assets.music1);
	}

	@Override
	public void dispose()
	{
		((Activity) game).runOnUiThread(new Runnable() {

			public void run()
			{
				nameScoreInput.setVisibility(EditText.GONE);
			}
		});
	}

}
